//
//  LBMSearchBar.swift
//  微博
//
//  Created by 刘保满 on 16/5/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class LBMSearchBar: UIView {

    //文本输入框
    @IBOutlet weak var searchTF: UITextField!
    
    //取消按钮
    @IBOutlet weak var cancelBtn: UIButton!
    //文本输入框的右边约束
    @IBOutlet weak var rightCon: NSLayoutConstraint!
    
    
    
    //加载xib 的类方法
    class func loadSearchBar() -> LBMSearchBar {
        
        let searchView = NSBundle.mainBundle().loadNibNamed("LBMSearchBar", owner: nil, options: nil).last as! LBMSearchBar
        
    return searchView
        
    }
    
    //重写此方法 
    override func awakeFromNib() {
        
        //设置宽度
        self.bounds.size.width = ScreenWidth
        
        //文本框左侧视图,还不会显示
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //左侧视图显示模式,总是显示
        searchTF.leftViewMode = .Always
        
//        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        
        //内容模式
        searchTF.leftView?.contentMode = .Center
        
        //设置圆角
        searchTF.layer.cornerRadius = 16
        searchTF.layer.masksToBounds = true
        
        //设置边线颜色
        searchTF.layer.borderColor = UIColor.lightGrayColor().CGColor
        
        //设置边线宽度
        searchTF.layer.borderWidth = 2
        
        
    }
    

    
    //文本输入框开始编辑时会来调用此方法
    @IBAction func textFiledEditingDidBegin(sender: AnyObject) {
        
        
        UIView.animateWithDuration(0.25) {
            
            //修改右边约束
            self.rightCon.constant = self.cancelBtn.bounds.width
            
            //强制刷新
            self.layoutIfNeeded()
            
        }
        
        
    }
    
    //取消按钮点击事件
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
        
        
        
        UIView.animateWithDuration(0.25) {
            
            self.rightCon.constant = 0
            
            self.layoutIfNeeded()
            
        }
        
        self.endEditing(true)
        
    }
    
}
